package com.itheima.springboottlias.controller;


import Pojo.PageResult;
import Pojo.Result;
import Pojo.Student;
import Pojo.StudentQueryParam;
import com.itheima.springboottlias.service.StudentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/students")
public class StudentController {
    @Autowired
    private StudentService studentService;
    // 分页和条件查询学员列表
    @GetMapping
    public Result query(StudentQueryParam studentQueryParam) {
        log.info("分页查询学员列表，参数：{}", studentQueryParam);
        PageResult pageResult = studentService.page(studentQueryParam);

        return Result.success(pageResult);
    }
    //批量删除学员
    @DeleteMapping("/{ids}")
    public Result removeById(@PathVariable List<Integer> ids) {
        log.info("根据id删除学员，id：{}", ids);
        studentService.remove(ids);
        return Result.success();
    }
    //添加学员
    @PostMapping
    public Result add(@RequestBody Student student) {
        log.info("添加学员，参数：{}", student);
        studentService.addStudent(student);
        return Result.success();
    }
    //根据id查询学员
    @GetMapping("/{id}")
    public Result findById(@PathVariable Integer id) {
        log.info("根据id查询学员，id：{}", id);
        Student student = studentService.findById(id);
        return Result.success(student);
    }
    //修改学员
    @PutMapping
    public Result update(@RequestBody Student student) {
        log.info("修改学员，参数：{}", student);
        studentService.updateStudent(student);
        return Result.success();
    }
    //违纪处理
    @PutMapping("/violation/{id}/{score}")
    public Result updateScoreById(@PathVariable Integer id, @PathVariable Integer score) {
        log.info("违纪处理，id：{}，score：{}", id, score);
        studentService.updateScore(id, score);
        return Result.success();
    }

}
